﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace gt.IdentityServer.Data
{
    public class AppUserTypeConfigurations : IEntityTypeConfiguration<AppUser>
    {
        public void Configure(EntityTypeBuilder<AppUser> builder)
        {
            builder.ToTable("t_app_user")
                .HasKey(x => x.Id);

            builder.HasIndex(x => x.UserName).IsUnique();

            builder.HasOne(u => u.Group)
                .WithMany(g => g.Users)
                .HasForeignKey(u => u.GroupId)
                .IsRequired(false)
                .OnDelete(DeleteBehavior.ClientSetNull);
        }
    }

    public class AppUserGroupTypeConfigurations : IEntityTypeConfiguration<AppUserGroup>
    {
        public void Configure(EntityTypeBuilder<AppUserGroup> builder)
        {
            builder.ToTable("t_app_group")
                .HasKey(x => x.Id);

            builder.HasIndex(x => x.Name).IsUnique();

            builder.HasMany(g => g.Users)
                .WithOne(u => u.Group);
        }
    }

    public class AppUserRoleTypeConfigurations : IEntityTypeConfiguration<AppUserRole>
    {
        public void Configure(EntityTypeBuilder<AppUserRole> builder)
        {
            builder.ToTable("t_app_role")
                .HasKey(r => r.Id);

            builder.HasIndex(r => r.Name).IsUnique();

            builder.HasMany(r => r.UserMappings)
                .WithOne(m => m.Role);

            builder.HasMany(r => r.GroupMappings)
                .WithOne(m => m.Role);
        }
    }

    public class AppUserRoleMappingTypeConfigurations : IEntityTypeConfiguration<AppUserRoleMapping>
    {
        public void Configure(EntityTypeBuilder<AppUserRoleMapping> builder)
        {
            builder.ToTable("t_app_userrole_mapping")
                .HasKey(m => new { m.UserId, m.RoleId });

            builder.HasOne(m => m.User)
                .WithMany(u => u.RoleMappings)
                .HasForeignKey(m => m.UserId)
                .OnDelete(DeleteBehavior.Cascade);

            builder.HasOne(m => m.Role)
                .WithMany(r => r.UserMappings)
                .HasForeignKey(m => m.RoleId)
                .OnDelete(DeleteBehavior.Cascade);
        }
    }

    public class AppGroupRoleMappingTypeConfigurations : IEntityTypeConfiguration<AppGroupRoleMapping>
    {
        public void Configure(EntityTypeBuilder<AppGroupRoleMapping> builder)
        {
            builder.ToTable("t_app_grouprole_mapping")
                .HasKey(m => new { m.GroupId, m.RoleId });

            builder.HasOne(m => m.Group)
                .WithMany(u => u.RoleMappings)
                .HasForeignKey(m => m.GroupId)
                .OnDelete(DeleteBehavior.Cascade);

            builder.HasOne(m => m.Role)
                .WithMany(r => r.GroupMappings)
                .HasForeignKey(m => m.RoleId)
                .OnDelete(DeleteBehavior.Cascade);
        }
    }
}
